package de.metalcon.gwt.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;

import de.metalcon.gwt.client.interactions.AInteraction;
import de.metalcon.gwt.client.mainDesign.MainContent;
import de.metalcon.gwt.client.mainDesign.MainHeader;
import de.metalcon.gwt.client.mainDesign.MainPanel;
import de.metalcon.gwt.client.pages.APage;
import de.metalcon.gwt.client.pages.HomePage;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Metalcon implements EntryPoint, ValueChangeHandler<String> {
	public void onModuleLoad() {
		StyleInjector.injectStylesheet(getBundle().css().getText());

		String initToken = History.getToken();
		if (initToken.length() == 0) {
			History.newItem(HomePage.generateHistoryToken());
		}

		
		MainPanel mainPanel = new MainPanel();
		RootPanel.get().add(mainPanel);
		MainContent content = new MainContent();
		mainPanel.setContent(content);

		History.addValueChangeHandler(this);
		History.fireCurrentHistoryState();
	}

	public static Bundle getBundle() {
		return GWT.create(InteriorBundle.class);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		// will use LocatoinAnalyzer to read the history token and open the
		// right page
		APage page = HistoryTokenAnalyzer.GeneratePageFromHistory();
		ArrayList<AInteraction> modules = HistoryTokenAnalyzer
				.GenerateInteractionsFromHistory();
		MainContent.OpenPage(page);
		MainContent.StartInteractions(modules);
	}
}
